<?php
require_once 'models/cms.inc';
require_once 'models/TMSACL.inc';
require_once 'util/translit.inc';
require_once 'util/seo.inc';

class TCMSPageItem extends TCandyItem {
	public function Associations(IItem $class){
		return $this->Model->AssociatedPages($this,$class);
	}
	
	public function  AgregationContainer(){
		return $this->Model->AgregationContainer($this);
	}
}

/**
 * 
 * @author kras
 * @property TMSACL $Acl
 * @property ITransliter $Transliter
 * @property ITitleGenerator $TitleGenerator
 * @property IKeyValueRepository $ShopSettings
 * @property TCMSPageAssociationModel $AssociationModel
 */

class TCMSPageModel extends TCandyModel {
/**
 * @var TMSACL
 */	
	protected $_ioc_acl_;
	
/**
 * @var ITransliter[]
 */	
	protected $_ioc_transliter_;
	
/**
 * @var ITitleGenerator
 */	
	protected $_ioc_title_generator_;

/**
 * @var IKeyValueRepository
 */
	protected $_ioc_shop_settings_;
	
/**
 * @var TCMSPageAssociationModel
 */	
	protected $_ioc_association_model_;
	
	private $_thumb_sizes_ = array();
	
	protected function afterConstruct(){
		$this->_conf_classname_ = 'TMSPage';
	}
	
	protected function postProcessProperties(){
		parent::postProcessProperties();
		$this->propertyMetas['Snippet']->Type = TItemPropertyType::PT_MULTILINE; 
		$this->propertyMetas['Keywords']->Type = TItemPropertyType::PT_MULTILINE; 
		$this->propertyMetas['Description']->Type = TItemPropertyType::PT_MULTILINE; 
		$this->propertyMetas['PageContents']->Type = TItemPropertyType::PT_HTML; 
		$this->propertyMetas['Added']->ReadOnly = true;
		$this->propertyMetas['Modified']->ReadOnly = true;
		$this->propertyMetas['Published']->ReadOnly = true;
		$this->propertyMetas['ModifiedBy']->ReadOnly = true;
		$selection = array();
		$modes = $this->Adapter->Fetch(new TMSShowMode());
		foreach ($modes as $mode)
			$selection[$mode->Id] = (string)$mode;
		$this->propertyMetas['Mode']->SetSelection($selection);
		
		$langsel = array($this->Application()->Language=>'application locale');
		if ($this->ShopSettings)
			if ($langs = $this->ShopSettings->GetValue('Languages')){
			 $langsel = TConvertions::ConvertToArray($langs,array(';','='),array(false,true));
		}
		
		$this->propertyMetas['Language']->SetSelection($langsel);
		
		unset($this->propertyMetas['Comments']);
		unset($this->propertyMetas['Categories']);
		unset($this->propertyMetas['AssociationClasses']);
	}
	
	protected function preEditItem($id,array $data, $origin){
		if (isset($data['IsPublished']) && $data['IsPublished'] != $origin->IsPublished){
			if ($data['IsPublished'])
				$data['Published'] = new TDate();
		}
		return $data;
	}	
		
	protected function beforeSave(TNOrmObject $object){
		$object = parent::beforeSave($object);
		if (!$object->Added)
			$object->Added = new TDate();
		$object->Modified = new TDate();
		if ($object->IsPublished && is_null($object->Published))
			$object->Published = new TDate();
		$u = $this->Acl->CurrentUser();
		if (($u instanceof TMSUser) && ($u->Roles & TMSRole::CONTENT_MANAGER))
			$object->ModifiedBy = $u;
		else throw new TCoreException(TCoreException::ERR_ACCESS_DENIED);
		if ((!$object->Title) && $this->TitleGenerator)
			$object->Title = $this->TitleGenerator->GenerateTitle($object);
		if (!$object->Language)
			$object->Language = $this->Application()->Language;		
		if (!$object->Slug){
			foreach ($this->Transliter as $t)
				if ($t->InputLanguage() == $object->Language){
					$object->Slug = strtolower(trim($t->Transliterate($object->Title)));
					$object->Slug = preg_replace('/[^\w]+$/','',$object->Slug);
					$object->Slug = str_ireplace(' ','-',$object->Slug);
				}
		}
		return $object;
	}
	
	public function WrapObject($object,  array &$caller_stack = array()){
		if ($object instanceof TMSCategoryPageAssociation)
			return parent::WrapObject($object->Category,$caller_stack);
		if ($object instanceof TMSPageAssociation)
			return parent::WrapObject($object->DetailPage,$caller_stack);
		if ($object instanceof TMSPage)
			return new TCMSPageItem($object, $this);	
		return parent::WrapObject($object,$caller_stack);
	}
	
	public function AssociatedPages(IItem $page, IItem $class){
		if ($this->AssociationModel)
			return $this->AssociationModel->PageAssociations($class,$page);
		return new TArrayIterator(array());
	}
	
	public function AgregationContainer(IItem $page){			
		if ($this->AssociationModel)
			return $this->AssociationModel->Container($page);
		return null;
	}
	
}